package org.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * DirectExchange 模式
 *
 *@ClassName: DirectRabbitConfig
 *@Author: CJ
 *@Date: 2021-9-23 17:05
 */
@Configuration
public class DirectRabbitConfig {

    @Bean
    public Queue DirectQueue() {
        /*durable:是否持久化，默认是false，持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在*/
        /*exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除*/
        /*autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除*/
        /*一般设置一下队列的持久化就好，其余两个就是默认false*/
        return new Queue("DirectQueue", true);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange", true, false);
    }

    /*将队列和交换机绑定, 并设置用于匹配键：DirectRouting*/
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(DirectQueue()).to(directExchange()).with("DirectRouting");
    }

    /*用于测试*/
    @Bean
    public DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange", true, false);
    }
}
